﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class SiteDao
    {


        public SiteDao()
        {

            Mapper.CreateMap<E_Site, SiteModel>();
        }
        public List<E_Site> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Site.Count();

                return edm.E_Site.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public SiteModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Site.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Site, SiteModel>(query);
            }

        }
        public SiteModel GetCurrentSite()
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Site.FirstOrDefault(s => s.IsCurrent == 1);
                return Mapper.Map<E_Site, SiteModel>(query);
            }
        }

        public int Save(SiteModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Site.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Site();

                    edm.AddToE_Site(query);

                }


                query.WebName = obj.WebName;

                query.WebKey = obj.WebKey;

                query.WebDesc = obj.WebDesc;

                query.WebEmail = obj.WebEmail;

                query.EmailPwd = obj.EmailPwd;

                query.WebLogo = obj.WebLogo; 
                
                query.WebLogoPath = obj.WebLogoPath;

                query.WebEName = obj.WebEName;

                query.WebEKey = obj.WebEKey;

                query.WebEDesc = obj.WebEDesc;

                query.EndTime = obj.EndTime;

                query.PayMethod = obj.PayMethod;
                query.FlowPic = obj.FlowPic;
                if (obj.Id > 0)
                {
                    query.CreateTime =Convert.ToDateTime( obj.CreateTime);
                }
                else {
                    query.CreateTime = DateTime.Now;
                }
                query.RegAgree = obj.RegAgree;

                query.IsDelete = obj.IsDelete;
                query.IsAsk = obj.IsAsk;
                query.CreateUserId = obj.CreateUserId;

                query.EmailReg = obj.EmailReg;

                query.StmpServer = obj.StmpServer;
                edm.SaveChanges();

                return query.Id;


            }

        }

        public List<SiteModel> GetAll()
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_Site>, List<SiteModel>>(edm.E_Site.Where(s=>s.IsDelete==0).ToList());
            }
        }
        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Site.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

